package sg.edu.nus.iss.phoenix.user.service;

import java.sql.*;
import java.util.*;

import sg.edu.nus.iss.phoenix.authenticate.dao.RoleDao;
import sg.edu.nus.iss.phoenix.core.dao.*;
import sg.edu.nus.iss.phoenix.core.exceptions.NotFoundException;
import sg.edu.nus.iss.phoenix.user.dao.UserDao;
//import sg.edu.nus.iss.phoenix.user.dao.impl.UserDaoImpl;
import sg.edu.nus.iss.phoenix.user.entity.User;

/**
 * This service class contains all methods and functions which handles the business logic for User objects
 * @author @author Dinh Ngoc Tuan Vu, Goh Zhe Yi
 *
 */
public class UserService {
	DAOFactoryImpl factory;
	UserDao udao;
	RoleDao rdao;

	/**
	 * Constructor of UserService class
	 */
	public UserService() {
		super();
		// TODO Auto-generated constructor stub
		factory = new DAOFactoryImpl();
		udao = factory.getUserDAO();
		rdao = factory.getRoleDAO();

	}
	
	/**
	 * This method gets all the presenters found in the system
	 * @return 
	 * 		List of Users with the role as presenter
	 */
	public List<User> getAllPresenters() {
		List<User> presentersList = new ArrayList<User>();
		try {
			presentersList = (ArrayList<User>) udao.getAllPresenters();

			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return presentersList;
	}
	
	
	/**
	 * This method gets all the producer found in the system
	 * @return 
	 * 		List of Users with the role as producer
	 */
	public List<User> getAllProducers() {
		List<User> producersList = new ArrayList<User>();
		try {
			producersList = (ArrayList<User>) udao.getAllProducers();

			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return producersList;
	}

	
	/*public User getUser(){
		user = userDAO.getUser(id);
		return user;
	}*/
	
	/**
	 * This method inserts the user object into the system
	 * @param userInput
	 * 			User object to be created
	 */
	public void createUser(User userInput) throws SQLException{
		try {
			udao.create(userInput);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new SQLException("Cannot create an existing user");
			//e.printStackTrace();
		}
	}
	
	/**
	 * This method saves the modified user object into the system
	 * @param userInput
	 * 			modified User object
	 */
	public void modifyUser(User userInput){
		try {
			udao.save(userInput);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * This method deletes the user object from the system
	 * @param userInput
	 * 			User object to be deleted
	 */
	public void deleteUser(User userInput) throws SQLException{
		try {
			udao.delete(userInput);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new SQLException("Cannot delete this user!");
			//e.printStackTrace();
		}
	}
	
	/**
	 * This method gets all the users found in the system
	 * @return 
	 * 		List of Users
	 */
	public List<User> getAllUser(){
		List<User> listUsers = new ArrayList<User>();
		try {
			listUsers = udao.loadAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listUsers;
	}
}
